package com.software.twinmanager.tcp;

import com.software.twinmanager.controller.MessageReceiver;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Arrays;

@Slf4j
public class TCPUtils {
    private static final int MAX_TRY = 1; //最大尝试次数，勿设为0
    private static final String ADDRESS = "192.168.2.1";
    private static final int PORT = 8890;

    private static TCPConnection connection = null;
    private static final MessageReceiver server = new MessageReceiver();

    private static boolean getConnection() {
        try {
            if (connection != null) {
                connection.close();
            }
            connection = new TCPConnection(ADDRESS, PORT, server);
        } catch (IOException e) {
            log.warn("TCP连接机器失败");
            return false;
        }
        return true;
    }

    private static boolean checkConnection() {
        int retry = 0;
        while (connection == null || connection.isClosed()){
            if (retry > MAX_TRY) return false;
            if (getConnection()) {
                return true;
            }
            retry ++;
        }
        return true;
    }

    public static void resetConnection() {
        getConnection();
    }

    public static boolean startBelt(byte num, byte speed) {
        if (checkConnection()){
            try {
                connection.startBelt(num, speed);
                return true;
            } catch (IOException e) {
                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean stopBelt(byte num) {
        if (checkConnection()){
            try {
                connection.stopBelt(num);
                return true;
            } catch (IOException e) {
                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean getCurrentInfo() {
        if (checkConnection()){
            try {
                connection.getCurrentInfo();
                return true;
            } catch (IOException e) {
                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean push(byte num, byte hold) {
        if (checkConnection()){
            try {
                connection.push(num, hold);
                return true;
            } catch (IOException e) {
                return false;
            }
        } else {
            return false;
        }
    }

    public static void sendCommand(byte[] command) {
        if (command[0] == 1) { //启动皮带
            if (startBelt(command[1], command[2])) {
                byte[] callback = {9, command[1], 1, 0, command[2], 0, 0, 0};
                server.appointSending("client", Arrays.toString(callback));
            }
            else {
                byte[] callback = {9, command[1], 1, 1, 0, 0, 0, 0};
                server.appointSending("client", Arrays.toString(callback));
            }
        }
        else if (command[0] == 2) { //停止皮带
            if (stopBelt(command[1])) {
                byte[] callback = {9, command[1], 2, 0, 0, 0, 0, 0};
                server.appointSending("client", Arrays.toString(callback));
            }
            else {
                byte[] callback = {9, command[1], 2, 1, 0, 0, 0, 0};
                server.appointSending("client", Arrays.toString(callback));
            }
        }
        else if (command[0] == 3) { //启动推杆
            if (push(command[1], command[3])) {
                byte[] callback = {10, command[1], 1, 0, command[3], 0, 0, 0};
                server.appointSending("client", Arrays.toString(callback));
            }
            else {
                byte[] callback = {10, command[1], 1, 1, command[3], 0, 0, 0};
                server.appointSending("client", Arrays.toString(callback));
            }
        }
        else if (command[0] == 4) { //查询状态
            if (getCurrentInfo()) {
                byte[] callback = {11, 0, 0, 0, 0, 0, 0, 0};
                server.appointSending("client", Arrays.toString(callback));
            }
            else {
                byte[] callback = {11, 1, 0, 0, 0, 0, 0, 0};
                server.appointSending("client", Arrays.toString(callback));
            }
        }
        else if (command[0] == 7) { //
            //TODO 复位机器
        }
    }
}
